package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsBusinessLabelDao;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsbusinessDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessLabel;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessTypeRelation;
import com.syni.mdd.yhd.common.all.entity.BmsVideo;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsBusinessLabelService extends BaseService<BmsBusinessLabel, Integer> {

    @Autowired
    BmsBusinessLabelDao bmsBusinessLabelDao;

    @Override
    public BmsBusinessLabelDao getBaseDao() {
        return bmsBusinessLabelDao;
    }

    public Map<String, Object> findLabelToReleaseVideo(Integer typeId,String labelName, Integer pageNum, Integer pageSize,
                                                       Integer recommendLabelSize) {
        List<Map<String,Object>> bmsBusinessLabels = new LinkedList<>();
        if(!"".equals(labelName)) {
            labelName = "%" + labelName + "%";
            Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.DESC, "new_time");
            if (typeId != null) {
                bmsBusinessLabels.addAll(bmsBusinessLabelDao.findByTypeIdAndLabelNameLikeAndIsDelete(typeId, labelName, Integer.parseInt(Constants.COMMON_FLAG_FALSE), pageable));
            }else {
                bmsBusinessLabels.addAll(bmsBusinessLabelDao.findByLabelNameLikeAndIsDelete(labelName, Integer.parseInt(Constants.COMMON_FLAG_FALSE), pageable));
            }
        }
        Map<String,Object> userDataMap = new HashMap<>();
        userDataMap.put("isNoResult",Constants.COMMON_FLAG_FALSE_INT);
        if(bmsBusinessLabels.size() == Constants.COMMON_FLAG_FALSE_INT){
            //搜索热门标签进行推荐
            userDataMap.put("isNoResult",Constants.COMMON_FLAG_TRUE_INT);
            Pageable pageable = BmsCommonService.getPageable(1,recommendLabelSize, Sort.Direction.DESC, "useTotal","newTime");
            Map<String,Object> conditionMap = new HashMap<>();
            conditionMap.put("isDelete",Constants.COMMON_FLAG_FALSE_INT);
            if(typeId != null){
                conditionMap.put("typeId",typeId);
            }
            List<BmsBusinessLabel> recommendLabels = bmsBusinessLabelDao.findAll(new SpecificationUtils(conditionMap),pageable).getContent();
            userDataMap.put("recommendLabels",recommendLabels);
        }
        return ResultMapHelper.success(bmsBusinessLabels,userDataMap, ResultMsg.MSG_GET_SUCCESS);
    }

    public Map<Integer, List<BmsBusinessLabel>> findHaveGroupBuyLabelByType(Set<Integer> typeIds,
                                                                            String addrCity,String addrDistrict) {
        Map<Integer,List<BmsBusinessLabel>> resultMap = new HashMap<>();
        if(typeIds.size() > 0) {
            List<BmsBusinessLabel> bmsBusinessLabels = bmsBusinessLabelDao.selfFindByTypeIds(typeIds,addrCity,addrDistrict);
            if (bmsBusinessLabels != null && bmsBusinessLabels.size() > 0) {
                for (BmsBusinessLabel bmsBusinessLabel : bmsBusinessLabels) {
                    if (!resultMap.containsKey(bmsBusinessLabel.getTypeId())) {
                        resultMap.put(bmsBusinessLabel.getTypeId(), new ArrayList<>());
                    }
                    resultMap.get(bmsBusinessLabel.getTypeId()).add(bmsBusinessLabel);
                }
            }
        }
        return resultMap;
    }

    public List<BmsBusinessLabel> findByIdIn(Set<Integer> labelIds) {
        return bmsBusinessLabelDao.findByIdIn(labelIds);
    }

    public void postBusinessLabelId(BmsBusiness bmsBusiness) {
        if(bmsBusiness != null){
            List<BmsBusiness> bmsBusinesses = new ArrayList<>();
            bmsBusinesses.add(bmsBusiness);
            postBusinessLabelId(bmsBusinesses);
        }
    }

    public void postBusinessLabelId(List<BmsBusiness> bmsBusinesses){
        if(bmsBusinesses != null && bmsBusinesses.size() > 0){
            Set<String> labels = bmsBusinesses.stream().map(BmsBusiness::getLabel).collect(Collectors.toSet());
            Map<String,BmsBusinessLabel> bmsBusinessLabelMap = new LinkedHashMap<>();
            List<BmsBusinessLabel> bmsBusinessLabels = bmsBusinessLabelDao.findByLabelNameIn(labels);
            if(bmsBusinessLabels.size() > 0){
                bmsBusinessLabels.forEach(bt -> {
                    bmsBusinessLabelMap.put(bt.getLabelName(),bt);
                });
            }
            bmsBusinesses.forEach(bu -> {
                if(bmsBusinessLabelMap.containsKey(bu.getLabel())){
                    bu.setLabelId(bmsBusinessLabelMap.get(bu.getLabel()).getId());
                }
            });
        }
    }
}
